﻿using System;
using System.Web.Mvc;
using ProjetoComperio.BusinessLogicLayer;
using ProjetoComperio.DataLayer.Entities;
using ProjetoComperio.Framework.Web.Security;
using ProjetoComperio.Framework.Web.Security.UrlEncrypt;
using ProjetoComperio.SystemFramework.Web;
using System.Collections.Generic;

namespace ProjetoComperio.Presentation.Web.Controllers
{
    //[RequiresAuthentication]
    public class ApplicationController : ComperioController
    {
        private ApplicationBLL applicationBLL;

        public ApplicationController()
        {
            applicationBLL = new ApplicationBLL();
        }

        public ApplicationController(ApplicationBLL applicationBLL)
        {
            this.applicationBLL = applicationBLL;
        }

        #region index

        [HttpGet]
        public ActionResult Index()
        {
            ViewData.Model = applicationBLL.RetrievApplications();
            return View();
        }

        #endregion

        #region MyIndex

        [HttpGet]
        public ActionResult MyIndex()
        {
            ViewData.Model = applicationBLL.RetrieveApplications(Convert.ToInt32(this.CurrentUser.PersonID));
            return View();
        }

        #endregion

        #region Create

        [HttpGet]
        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Create()
        {
            return View();
        }

        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "ContribuitionId")]Application application)
        {

            AcademicPersonBLL academicPersonBLL = new AcademicPersonBLL();
            application.AcademicPerson = academicPersonBLL.RetrieveAcademicPersonByID(Convert.ToInt32(this.CurrentUser.PersonID));
            //application.AcademicPersonID = Convert.ToInt32(Convert.ToInt32(this.CurrentUser.PersonID));
            applicationBLL.Create(application);

            return RedirectToAction("MyIndex");
        }

        #endregion

        #region Edit

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit([Bind(Exclude = "ContribuitionId")] Application application)
        {
            var _application = applicationBLL.RetrieveApplication(Convert.ToInt32(this.CurrentUser.PersonID));


            _application.Title = application.Title;
            _application.RegisterDate = application.RegisterDate;
            _application.SourceInfo = application.SourceInfo;
            _application.Description = application.Description;


            _application.IsOpenSource = application.IsOpenSource;
            _application.IsFree = application.IsFree;
            _application.UseInstructions = application.UseInstructions;

            applicationBLL.Edit(_application);

            return RedirectToAction("Index");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public ActionResult Edit(String id)
        {            
            var Application = applicationBLL.RetrieveApplication(Convert.ToInt32(this.CurrentUser.PersonID));
            return View(Application);
        }
        #endregion

        #region Details
        [HttpGet]
        public ActionResult Details(string id)
        {
            var entity = applicationBLL.RetrieveApplication(Convert.ToInt32(URLEncrypt.DecryptFromString(id)));
            return View(entity);
        }
        #endregion
                
    }
}
